package elitech.vietnam.myfashion.extend;

import java.io.IOException;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.LoginActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.dialog.WarningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Post;
import elitech.vietnam.myfashion.fragment.DetailFragment;
import elitech.vietnam.myfashion.fragment.StylerFragment;
import elitech.vietnam.myfashion.view.StylerDetailView;

public class GridStylerArrayAdapter extends ArrayAdapter<Post> {
	private MainActivity mContext;
	
	public GridStylerArrayAdapter(MainActivity context, int resource, int textViewResourceId, List<Post> objects) {
		super(context, resource, textViewResourceId, objects);
		this.mContext = context;
	}
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
	
	static class ViewHolder {
		ImageView image;
		TextView text;
		LinearLayout layLikes;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final Post post = getItem(position);
		final ViewHolder holder;
		boolean isMyStyle = false;
		
		if (mContext.mUser != null && post.IdAccount == mContext.mUser.Id)
			isMyStyle = true;
		
		if (row == null) {
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.item_styler, null, false);
			holder = new ViewHolder();
			
			holder.image = (ImageView) row.findViewById(R.id.iproduct_image);
			holder.layLikes = (LinearLayout) row.findViewById(R.id.iproduct_layBtnLike);
			holder.text = (TextView) row.findViewById(R.id.iproduct_txtLikes);
			row.setTag(holder);
		}else
			holder = (ViewHolder) row.getTag();
		
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_THUMB_URL + post.image_url, R.drawable.loading300);
		holder.text.setText(post.Likes+"");
		
		if (post.Liked())
			holder.layLikes.setBackgroundResource(R.drawable.liked);
		else
			holder.layLikes.setBackgroundResource(R.drawable.like);
		
		final boolean b = isMyStyle;
		holder.image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mContext.mStylerFragment == null)
					mContext.mStylerFragment = new StylerFragment();
				if (!mContext.mStylerFragment.isAdded()) {
					mContext.mStylerFragment.setPost(post, b);
					mContext.replaceFragment(mContext.mStylerFragment, true);
				}
			}
		});
		
		holder.layLikes.setOnClickListener(new View.OnClickListener() {
			boolean isProcessing = false;
			@Override
			public void onClick(View v) {
				if(!isProcessing){
					if(mContext.mUser == null){
						new OkCancelWarnningDialog(mContext, "You must login"){
							public void onOk() {
								Intent i = new Intent(mContext, LoginActivity.class);
								mContext.startActivityForResult(i, MainActivity.LOGINACTIVITY);
							};
						}.show();
					}else{
						if(post.Liked()){
							new OkCancelWarnningDialog(mContext, "Do you want to unlike this product?"){
								public void onOk() {
									new CallServiceTask(){
										private int rs = -2;
										protected void onPreExecute() {
											isProcessing = true;
										};
										public void work() {
											try {
												rs = service.newLike(mContext.mUser.Id, post.Id, post.Liked(), 2);
											} catch (IOException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											} catch (XmlPullParserException e) {
												// TODO Auto-generated catch block
												e.printStackTrace();
											}
										};
										protected void onPostExecute(Void result) {
											if (rs == -2) {
												new WarningDialog(
														mContext,
														mContext.getString(R.string.connecterror))
														.show();
											} else if (rs == -1) {
												post.Likes = post.Likes-1;
												holder.text.setText(post.Likes + "");
												holder.layLikes.setBackgroundResource(R.drawable.like);
												post.Liked = 0;
											} else {
												holder.text.setText(rs + "");
												post.Likes = rs;
												holder.layLikes.setBackgroundResource(R.drawable.like);
												post.Liked = 0;
											}
											isProcessing = false;
										};
									}.execute();
								};
							}.show();
						}else{
							new CallServiceTask(){
								private int rs = -2;
								protected void onPreExecute() {
									isProcessing = true;
								};
								public void work() {
									try {
										rs = service.newLike(mContext.mUser.Id, post.Id, post.Liked(), 2);
									} catch (IOException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									} catch (XmlPullParserException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								};
								protected void onPostExecute(Void result) {
									if (rs == -2) {
										new WarningDialog(
												mContext,
												mContext.getString(R.string.connecterror))
												.show();
									} else if (rs == -1) {
										post.Likes = post.Likes+1;
										holder.text.setText(post.Likes + "");
										post.Liked = 1;
										holder.layLikes.setBackgroundResource(R.drawable.liked);
									} else {
										holder.text.setText(rs + "");
										post.Likes = rs;
										holder.layLikes.setBackgroundResource(R.drawable.liked);
										post.Liked = 1;
									}
									isProcessing = false;
								};
							}.execute();
						}
					}
				}
			}
		});
		
		return row;
	}
	
}

